package com.itheima.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.admin.pojo.Dish;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/15 15:17
 * @PackageName:com.itheima.mapper
 * @ClassName: DishMapper
 * @Description: TODO
 * @Version 1.0
 */
@Mapper
public interface DishMapper extends BaseMapper<Dish> {
    /**
     *  默认查询总条数
     * @param type
     * @return
     */
    @Select("select count(*) from dish where type = #{type}")
    int selectCount(int type);

    /**
     * 默认分页查询菜品
     * @param offset
     * @param pageSize
     * @param type
     * @return
     */
    @Select("select id,type,name,category_id,price,code,image,status,sort,update_time," +
            "(select name from category where  category.id = dish.category_id ) category_name " +
            "from dish where type = #{type} limit #{offset},#{pageSize}")
    List<Dish> selectAll(int offset, int pageSize, int type);

    /**
     * 按名字模糊查询计算总条数
     * @param type
     * @param name
     * @return
     */
    @Select("select count(*) from dish where type = #{type} and name like CONCAT ('%', #{name},'%') ")
    int selectCountByName(int type, String name);

    /**
     * 按名字分页模糊查询
     * @param offset
     * @param pageSize
     * @param type
     * @param name
     * @return
     */
    @Select("select id,type,name,category_id,price,code,image,status,sort,update_time," +
            "(select name from category where  category.id = dish.category_id) category_name " +
            "from dish where type = #{type} and name like CONCAT ('%', #{name},'%') limit #{offset},#{pageSize}")
    List<Dish> selectByName(int offset, int pageSize, int type, String name);


}
